/*   
 * Copyright (c) 2011, TUgether, TU Braunschweig
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
 /**
 * @author Mohamed Karray <mohamed.karray@gmx.de>
 *
 */
 

package de.tu_bs.tugether.liferayunit.liferayElements;

import java.io.IOException;

import com.gargoylesoftware.htmlunit.html.HtmlElement;

import de.tu_bs.tugether.liferayunit.exceptions.LiferayElementNotFoundException;
import de.tu_bs.tugether.liferayunit.exceptions.LiferayUnitException;
import de.tu_bs.tugether.liferayunit.liferayPages.LiferayPage;

public class ChatMessage extends LiferayElement {

	protected static final String XPATH_KEY_AUTOR = "autor";
	protected static final String XPATH_KEY_TIME = "time";
	protected static final String XPATH_KEY_MESSAGE = "message";

	String autor;
	String time;
	String message;

	public ChatMessage(HtmlElement htmlElement, LiferayPage lfrPage) throws IOException,
			LiferayElementNotFoundException {
		super(htmlElement, lfrPage);
	}

	public String getAutor() {
		return autor;
	}

	public String getTime() {
		return time;
	}

	public String getMessage() {
		return message;
	}

	@Override
	protected void initialize() throws LiferayElementNotFoundException {
		try {
			HtmlElement autor_el = this.getFirstByXPathKey(XPATH_KEY_AUTOR);
			this.autor = autor_el.getTextContent();
		} catch (LiferayUnitException e) {
			this.autor = null;
		}
		try {
			HtmlElement time_el = this.getFirstByXPathKey(XPATH_KEY_TIME);
			this.time= time_el.getTextContent();
		} catch (LiferayUnitException e) {
			this.time = null;
		}
		try {
			HtmlElement message_el = this.getFirstByXPathKey(XPATH_KEY_MESSAGE);
			this.message = message_el.getTextContent();
		} catch (LiferayUnitException e) {
			this.message = null;
		}
	}

	public boolean equals(ChatMessage cm){
		return this.autor.equals(cm.getAutor()) && 
			   this.time.equals(cm.getTime()) && 
			   this.message.equals(cm.getMessage()); 
	}
	
}
